package soulkun.library.test

import java.io.*
import java.nio.charset.StandardCharsets

private fun main() {
    change(
        File("C:\\Users\\1\\Desktop\\隐私政策.txt"),
        File("C:\\Users\\1\\Desktop\\隐私政策.html")
    )
}

@Throws(Exception::class)
private fun change(source: File?, html: File?) {
    val fis = FileInputStream(source)
    val fos = FileOutputStream(html)
    val isr = InputStreamReader(fis, "gbk")
    val osw = OutputStreamWriter(fos, StandardCharsets.UTF_8)
    val buffer = CharArray(1024)
    var len: Int
    val builder = StringBuilder()
    while (isr.read(buffer).also { len = it } != -1) {
        builder.append(String(buffer, 0, len))
    }
    var text = builder.toString()
    text = text.replace("\r".toRegex(), "")
    text = text.replace("\n\n+".toRegex(), "<br>\n")
    text = text.replace("\n".toRegex(), "<br>\n")
    osw.write(text)
    osw.close()
    isr.close()
}
